package com.example.connecting_dots_android;


import java.util.List;

import android.app.Activity;
import android.app.ActivityManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends Activity implements ServiceConnection {

	// indicates whether the activity is linked to service player.
	private boolean mIsBound = false;

	// Saves the binding instance with the service.
	protected static MusicService mServ;
	
	private boolean mIsMute = false;
	private Button mHighScores;
	private Button mStartGame;
	private Button mSettings;
	private Button mGameRules;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_connecting_dots);	
		init();	
		settings();
	}

	/**
	 * method to initialize class members
	 * */
	private void init() {
		mIsMute = Utils.getIsMute(this);
		
		mHighScores = (Button) findViewById(R.id.idBtnHighScores);
		mStartGame = (Button) findViewById(R.id.idBtnNewGame);
		mSettings = (Button) findViewById(R.id.idBtnSettings);
		mGameRules = (Button) findViewById(R.id.idBtnGameRules);
		
		mHighScores.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				highScores(v);
			}
		});

		mStartGame.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				startGame(v);
			}
		});
		
		mSettings.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				settings(v);
			}
		});
		
		mGameRules.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				gameRules(v);
			}
		});
	}

	/**
	 * method to manage background and the music
	 * */
	private void settings() {
		// Starting the service of the player, if not already started.
		Intent music = new Intent(this, MusicService.class);
		startService(music);
		doBindService();
		
		Utils.setBackgroundImage(this, findViewById(R.id.mainMenuLayout));
	}

	/**
	 * method to go to the PlayeresNameAndSignPick activity
	 * */
	public void startGame(View view) {
		Intent intent = new Intent(this, PlayeresNameAndSignPick.class);
		startActivity(intent);
	}

	/**
	 * method to go to the HighScores activity
	 * */
	public void highScores(View view) {
		Intent intent = new Intent(this, HighScores.class);
		startActivity(intent);
	}
	
	/**
	 * method to go to the GameRules activity
	 * */
	public void gameRules(View view) {
		Intent intent = new Intent(this, GameRules.class);
		startActivity(intent);
	}

	/**
	 * method to go to the Settings activity
	 * */
	public void settings(View view) {
		Intent intent = new Intent(this, Settings.class);
		startActivity(intent);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}


	/** 
	 * interface connection with the service activity
	 * */
	public void onServiceConnected(ComponentName name, IBinder binder) {
		mServ = ((MusicService.ServiceBinder) binder).getService();
		if (mIsMute) {
			mServ.start();
		}
	}

	public void onServiceDisconnected(ComponentName name) {
		mServ = null;
	}

	/** 
	 * local methods used in connection/disconnection activity with service.
	 * */
	public void doBindService() {
		// activity connects to the service.
		Intent intent = new Intent(this, MusicService.class);
		bindService(intent, this, Context.BIND_AUTO_CREATE);
		mIsBound = true;
	}

	public void doUnbindService() {
		// disconnects the service activity.
		if (mIsBound) {
			unbindService(this);
			mIsBound = false;
		}
	}

	@Override
	protected void onResume() {
		super.onResume();
		mIsMute = Utils.getIsMute(this);
		Utils.setBackgroundImage(this, findViewById(R.id.mainMenuLayout));
		if (mServ!= null && mIsMute && !mServ.isPlaying()) {
			mServ.resume();
		}
	}

	@Override
	protected void onPause() {
		super.onPause();
		mIsMute = Utils.getIsMute(this);
		if (mServ!= null && mIsMute) {
			mServ.pause();
		}
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
		doUnbindService();
	}
	
	@Override
    public void onBackPressed() {
		ActivityManager mngr = (ActivityManager) getSystemService( ACTIVITY_SERVICE );

		List<ActivityManager.RunningTaskInfo> taskList = mngr.getRunningTasks(10);

		if(taskList.get(0).numActivities >1 ){
			Toast.makeText(this, "Press Back again to Exit.",
                    Toast.LENGTH_SHORT).show();
		}
		
		 finish();
    }
}
